var cols = document.querySelectorAll('.col');
var chess = document.querySelector('.chess');
var msg = document.querySelector('.message');
var message = document.querySelector('.messageContainer');
var toolTip = document.querySelector('h2');
var btn = document.querySelector('button');
var winsArr = [
    [0, 1, 2], [3, 4, 5], [6, 7, 8],
    [0, 3, 6], [1, 4, 7], [2, 5, 8],
    [0, 4, 8], [2, 4, 6]
];
var Player;
(function (Player) {
    Player["X"] = "x";
    Player["O"] = "o";
})(Player || (Player = {}));
var currentPlayer;
var step;
function handleClick(e) {
    var target = e.target;
    target.classList.add(currentPlayer);
    // 判断是否已经赢了
    if (isWin(currentPlayer)) {
        toolTip.innerHTML = "\u73A9\u5BB6 " + currentPlayer + " \u8D62\u4E86\uFF01\uFF01\uFF01";
        message.style.display = 'block';
        return;
    }
    else if (step >= 8) {
        toolTip.innerHTML = "\u8FD9\u4E00\u628A\u5E73\u5C40\uFF01\uFF01\uFF01";
        message.style.display = 'block';
        return;
    }
    // 切换玩家
    currentPlayer = currentPlayer === Player.X ? Player.O : Player.X;
    // 切换提示
    chess.classList.remove(Player.X, Player.O);
    chess.classList.add(currentPlayer);
    step++;
}
function isWin(player) {
    return winsArr.some(function (item) {
        var flag = 0;
        item.forEach(function (sub) {
            if (cols[sub].classList.contains(player)) {
                flag++;
            }
        });
        if (flag === 3) {
            return true;
        }
        return false;
    });
}
function startGame() {
    // 关闭遮盖层
    message.style.display = 'none';
    // 计数归零
    step = 0;
    // 重置玩家
    currentPlayer = Player.X;
    // 重置提示
    chess.classList.remove(Player.X, Player.O);
    chess.classList.add(currentPlayer);
    // 对格子的进行初始化操作
    cols.forEach(function (item) {
        var col = item;
        // 移除x, o类
        col.classList.remove(Player.X, Player.O);
        // 解绑上一次的点击事件
        col.removeEventListener('click', handleClick);
        // 重新绑定点击事件
        col.addEventListener('click', handleClick, { once: true });
    });
}
// 开始游戏
startGame();
// 重新开始游戏
btn.addEventListener('click', startGame);
